package com.prime.tutorial.moviestore.dao;


import com.prime.tutorial.moviestore.domain.Device;
import com.prime.tutorial.moviestore.domain.Model;
import com.prime.tutorial.moviestore.domain.ModelType;
import com.prime.tutorial.moviestore.domain.State;
import com.prime.tutorial.moviestore.Constants;
import org.springframework.stereotype.Repository;
import org.hibernate.*;

import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

@Repository
public class DeviceDAOWithJPA extends GenericDAOWithJPA<Device, Long> implements DeviceDAO {

    //	@SuppressWarnings("unchecked")
    public List<Device> findByName(String name) {
        Query query = entityManager.createQuery("Select distinct m from Device m where m.name = ?1");
        query.setParameter(1, name);
        return query.getResultList();
//		return getJpaTemplate().find("Select m from Groups m where m.name = ?1",name);
    }

    public List<Device> findByState(State state) {
        Query query = entityManager.createQuery("Select distinct d from Device d, State s where d.state = '"+state.getId()+"'");
//        query.setParameter(1, state.getId());
        return query.getResultList();
    }

    public List<Device> findByDeviceType(ModelType modelType) {
        Query query = entityManager.createQuery("Select distinct d from Device d, Model m, ModelType mt where d.model = m.id and m.modelType = ?1");
        query.setParameter(1, modelType.getId());
        return query.getResultList();
    }

    public List<Device> findByModelTypeAndState(ModelType modelType, State state) {
        Query query = entityManager.createQuery("Select distinct d from Device d, Model m, ModelType mt, State s where d.model = m.id and m.modelType ='"+modelType.getId()+"' and d.state = '"+state.getId()+"'");
//        query.setParameter(1, modelType.getId());
//        query.setParameter(2, state.getId());
        return query.getResultList();
    }

    public List<Device> findByModelAndState(Model model, State state) {
        Query query = entityManager.createQuery("Select distinct d from Device d, Model m, State s where d.model = '"+model.getId()+"' and d.state = '"+state.getId()+"'");
//        query.setParameter(1, modelType.getId());
//        query.setParameter(2, state.getId());
        return query.getResultList();
    }

    public List<Device> findAllEnabledNotCPE() {
        Query query = entityManager.createQuery("Select distinct d from State s, Device d,Model m, ModelType mt where d.state = s.id and s.type = '"+Constants.STATE_ENABLED+"' and d.model = m.id and m.modelType = mt.id and not mt.type = '"+Constants.MODELTYPE_CPE+"'");
        return query.getResultList();
    }

    public List<Device> findByState(List<State> states) {
        Query query = entityManager.createQuery("Select distinct d from Device d, State s where d.state in(:states)");
        query.setParameter("states", states);
        return query.getResultList();
    }

    public List<Device> repWorkload() {
       Query query = entityManager.createQuery("Select distinct d from Device d,DeviceTypeProperty dtp where d.model.id = dtp.model.id and dtp.property.type = '"+Constants.PROPERTY_PORTCOUNT+"' and cast(dtp.property_value, int)*0.9> " +
                "(select count(dtd.deviceport_from.device) from DeviceToDevice dtd where dtd.deviceport_to.device.id = d.id) ");
        return query.getResultList();
    }
}